package applogic;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Meeting extends Appointment
{
	private ArrayList<User> listOfAttendees;
	private int roomID;
	private int extAttendees;
	private String status;
	private int cancel;
	private String decliners = "";
	private int declinercount;
	

	public Meeting()
	{
		super();
		listOfAttendees = new ArrayList<User>();
		roomID = 0;
		extAttendees = 0;
		status = "waiting";
		declinercount = 0;
	}
	

	public Meeting(int ID, int day, int month, int start, int end,
					String description, int roomID, int extAttendees, int cancel, User holds, ArrayList<User>listOfAttendees, String status)
	{
		super(ID,day,month,start,end,description,"",holds);
		this.roomID = roomID;
		this.extAttendees = extAttendees;
		this.listOfAttendees = listOfAttendees;
		if(cancel==1)this.status = "canceled";
		else this.status = status;
		this.cancel = cancel;
		declinercount = 0;
		if(listOfAttendees==null)this.listOfAttendees = new ArrayList<User>();
	}
	
	public void buildFromNetMsg(String content)
	{
		listOfAttendees = new ArrayList<User>();
		StringTokenizer st = new StringTokenizer(content,",");
		
		//Construct meeting from message.
		day = Integer.parseInt(st.nextToken());
		month = Integer.parseInt(st.nextToken());
		start = Integer.parseInt(st.nextToken());
		end = Integer.parseInt(st.nextToken());
		ID = Integer.parseInt(st.nextToken());
		String tmp = st.nextToken("").substring(1);
		description = tmp.substring(0,64);
		description = description.substring(0,description.lastIndexOf(" "));
		st = new StringTokenizer(tmp.substring(64),",");
		owner = new User(st.nextToken(),"");
		extAttendees = Integer.parseInt(st.nextToken());
		roomID = Integer.parseInt(st.nextToken());
		status = st.nextToken();
		if(status.equals("canceled"))cancel=1;
		while(st.hasMoreElements())listOfAttendees.add(new User(st.nextToken(),""));
		declinercount = 0;
	}
	
	public ArrayList<User> getAttendees()
	{
		return listOfAttendees;
	}

	public void addUser(User user)
	{
		if(listOfAttendees==null)listOfAttendees=new ArrayList<User>();
		listOfAttendees.add(user);
	}

	public void removeUser(User user)
	{
		listOfAttendees.remove(user);
	}

	public void setExtAttendees(int externals)
	{
		extAttendees = externals;
	}
	
	public int getRoom()
	{
		return roomID;
	}
	
	public int getStart()
	{
		return start;
	}
	
	public int getEnd()
	{
		return end;
	}

	public void notifyCancel()
	{
		/* TODO: */
	}

	public void cancelMeeting()
	{
		/* TODO: */
	}
	
	public String toString()
	{
		String users = "";
		if(listOfAttendees!=null)
			for(User user: listOfAttendees)
				users+=user+",";
		return  "ID: "+ID+"\n"
				+"Date: "+day+" of "+month+"\n"
				+"From "+start+" to "+end+"\n"
				+"Description: "+description+"\n"
				+"At: "+roomID+"\n"
				+"Attendees: "+users+"\n"
				+"Ext. attendees: "+extAttendees+"\n"
				+"Status: "+status+"\n"
				+"Holder: "+owner;
	}

	public String toMessage() {
		char[] descrpt = new char[64];
		int a=0;
		for(;a<description.length()&&a<64;a++)descrpt[a]=description.charAt(a);
		for(;a<64;a++)descrpt[a]=32;
		
		String msg = ""+day+","+month+","+start+","+end+","+ID+","+new String(descrpt)+","+owner+","+extAttendees+","+roomID+","+status;
		if(listOfAttendees!=null)for(User user: listOfAttendees)msg+=","+user;
		else msg+=",NOBODY";
		return msg;
	}
	
	public String toSQL(){
		return "INSERT INTO Meeting(AID, Day, Month, Start, Finish, Description, cancel, ExternalAttendees, isin, holds)" +
				"VALUES("+ID+","+day+","+month+","+start+","+end+",'"+description+"',"+cancel+","+extAttendees+","+roomID+",'"+owner+"')";
	}

	public int getNumOfAttendees() {
		return listOfAttendees.size()+extAttendees;
	}
	
	
	
	public void setRoom(int room)
	{
		this.roomID = room;
	}

	public void setStatus(String status) {
		if(cancel==1)return;
		this.status = status;
		
	}
	public void cancel() {
		status = "canceled";
		cancel = 1;
	}

	public int getCanceled() {
		return cancel;
	}

	public int getExternalAttendees() {
		return extAttendees;
	}

	public String getStatus() {
		return status;
	}
	
	
	public void addDecliner(User user) {
		decliners+=","+user;
		declinercount++;
	}
	
	public String getDecliners()
	{
		return decliners;
	}
	
	public int getDeclinerCount()
	{
		return declinercount;
	}
}